package com.stenio.fancat.controller.baby;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.PageList;
import com.stenio.fancat.domain.baby.BabyAccount;
import com.stenio.fancat.domain.baby.BabyIconImage;
import com.stenio.fancat.domain.bo.UserRelationshipBO;
import com.stenio.fancat.domain.shop.FcShop;
import com.stenio.fancat.domain.user.UserRelationshipName;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.baby.BabyAccountForm;
import com.stenio.fancat.form.baby.BabyRelationshipForm;
import com.stenio.fancat.form.user.UserRelationshipForm;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.service.baby.BabyAccountService;
import com.stenio.fancat.vo.BabyAccountVO;
import com.stenio.fancat.vo.UserRelationshipVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宝宝信息展示
 */
@RestController
@RequestMapping("babyAccount")
public class BabyAccountController {

    @Autowired
    private BabyAccountService babyAccountService;

    @Autowired
    private BabyIconImageMapper babyIconImageMapper;

    /**
     * 增加宝宝
     * @param babyAccountForm
     * @param userRelationshipForm
     * @return
     */
    @PostMapping(value = "/insertBaby",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<BabyAccountVO> insertBabyAccountByCondition(@RequestBody(required = false) BabyAccountForm babyAccountForm,
                                                              @RequestBody(required = false) UserRelationshipForm userRelationshipForm){
        Result<BabyAccountVO> result = new Result();
        BabyAccountVO babyAccount = null;
        try {
            babyAccount = babyAccountService.insertBabyAccountByCondition(babyAccountForm,
                    userRelationshipForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(babyAccount);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 查询宝宝
     * @param userRelationshipForm
     * @return
     */
    @PostMapping(value = "/selectBaby",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<BabyAccountVO> selectBabyAccountByCondition(@RequestBody(required = false) UserRelationshipForm userRelationshipForm){
        Result<BabyAccountVO> result = new Result();
        BabyAccountVO babyAccount = null;
        try {
            babyAccount = babyAccountService.selectBabyAccountByCondition(userRelationshipForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(babyAccount);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 修改宝宝信息
     * @param userRelationshipForm,babyAccountForm
     * @return
     */
    @PostMapping(value = "/modifyBaby",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<BabyAccount> modifyBabyAccountByCondition (@RequestBody(required = false) UserRelationshipForm userRelationshipForm,
                                                             @RequestBody(required = false) BabyAccountForm babyAccountForm){
        Result<BabyAccount> result = new Result();
        BabyAccount babyAccount = null;
        try {
            babyAccount = babyAccountService.modifyBabyAccountByCondition(userRelationshipForm,babyAccountForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(babyAccount);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 查询我的个人信息页面（包含亲人的列表）
     * @param userRelationshipForm
     * @return
     */
    @PostMapping(value = "/selectUserList",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<UserRelationshipVO>> selectUserListByBabyId (@RequestBody(required = false) UserRelationshipForm userRelationshipForm){
        Result<List<UserRelationshipVO>> result = new Result<>();
        List<UserRelationshipBO> list = null;
        try {
            list = babyAccountService.selectUserListByBabyId(userRelationshipForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        List<UserRelationshipVO> userRelationshipVOList = new ArrayList<>();
        list.stream().forEach(bo -> {
            UserRelationshipVO userRelationshipVO = new UserRelationshipVO();
            try {
                BeanUtils.copyProperties(userRelationshipVO,bo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            userRelationshipVOList.add(userRelationshipVO);
        });

        result.setData(userRelationshipVOList);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 查询所有关系的名称
     * @return
     */
    @PostMapping(value = "/selectNames",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<UserRelationshipName>> selectNames(){
        Result<List<UserRelationshipName>> result = new Result<>();
        List<UserRelationshipName> list = babyAccountService.selectNames();
        result.setData(list);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    @PostMapping(value = "/selectBabyList",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<BabyAccountVO>> selectBabyList(@RequestBody UserRelationshipForm userRelationshipForm){
        Result<List<BabyAccountVO>> result = new Result<>();
        try {
            result.setData(babyAccountService.selectBabyList(userRelationshipForm.getUserId()));
            result.setFlag(Result.FLAG.success);
            return result;
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
    }

    @PostMapping("onOff/{id}/{status}")
    public Result onOff(@PathVariable String id, @PathVariable Integer status){
        Result<BabyAccount> result = new Result();
        try {
            result.setData(babyAccountService.onOff(id,status));
        } catch (Exception e) {
            result.setFlag(Result.FLAG.failure);
            result.setMsg("账户状态不合法");
        }
        return result;
    }

    @GetMapping("get/{id}")
    public Result get(@PathVariable String id,Integer pageNo,Integer pageSize) {
        Result result = new Result();
        PageList byBabyId = babyAccountService.getByBabyId(id, pageNo, pageSize);
        if(byBabyId == null) {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(byBabyId);
        return result;
    }

    @PostMapping("findbabyInfo")
    public Result findbabyInfo(@RequestBody BabyAccountForm babyAccountForm) {
        Result result = new Result();
        System.out.println(babyAccountForm);
        final Page<BabyAccount> babyAccountPage = babyAccountService.selectBabyInfo(babyAccountForm);
        Map<String,Object> data = new HashMap<>();
        if(babyAccountPage != null) {
            data.put("current",babyAccountPage.getCurrent());
            data.put("pages",babyAccountPage.getPages());
            data.put("size",babyAccountPage.getSize());
            data.put("total",babyAccountPage.getTotal());
            List<BabyAccountVO> babyAccountVOS = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(babyAccountPage.getRecords())) {
                babyAccountPage.getRecords().stream().forEach(baby->{
                    babyAccountVOS.add(babyToVo(baby));
                });
            }
            data.put("records",babyAccountVOS);
        }

        result.setData(data);
        return result;
    }

    private BabyAccountVO babyToVo(BabyAccount babyAccount) {
        BabyAccountVO babyAccountVO = new BabyAccountVO();
        org.springframework.beans.BeanUtils.copyProperties(babyAccount,babyAccountVO);
        BabyIconImage babyIconImage = babyIconImageMapper.selectByBid(babyAccount.getBabyId());
        babyAccountVO.setImage(babyIconImage.getImage());
        return babyAccountVO;
    }
}
